<template>
  <el-container>
    <el-header>
      <el-col :span="6" :xs="0" :sm="5" :md="6" :lg="6"><img src="https://fishandbearpaw.oss-cn-hangzhou.aliyuncs.com/systemImg/jdlogo.png"></el-col>
      <el-col :span="10" :xs="12" :sm="9" :md="8" :lg="6">
        <el-menu :default-active="activeIndex2" class="el-menu-demo menu" router mode="horizontal" @select="handleSelect"
          background-color="#57B8A0" text-color="#fff" active-text-color="#ffd04b">
          <el-menu-item :index="'/components/Main'">首页</el-menu-item>
          <el-menu-item :index="'/components/Create'" v-show="isLogin">发布</el-menu-item>
          <el-menu-item :index="'/components/Message'" v-show="isLogin" @click="clearMessage()">消息&nbsp;<el-avatar
              :size="20" v-show="hasMessage" style="background-color:red;">{{messageCount}}</el-avatar>
          </el-menu-item>
        </el-menu>
      </el-col>
      <el-col :span="3" :xs="5" :sm="3" :md="2" :lg="3">
        <el-input v-model="searchInput" placeholder="请输入内容">
        </el-input>
      </el-col>
      <el-col :span="2" :xs="2" :sm="2" :md="1" :lg="1">
        <el-button @click="search()" icon="el-icon-search" type="warning" circle></el-button>
      </el-col>
      <el-col :span="2" :xs="3" :sm="3" :md="2" :lg="1">
        <div class="block" v-show="isLogin">
          <el-avatar :size="50" shape="square" :src="headPic"></el-avatar>
        </div>
      </el-col>
      <el-col :xs="0" :sm="2" :md="2" :lg="2">
        <div class="userInfo" v-show="isLogin">{{userName}}</div>
      </el-col>
      <el-col :xs="2" :sm="2" :md="2" :lg="1">
        <el-button type="text" class="userInfo" v-show="isLogin" @click="signOut">[退出]</el-button>
      </el-col>
      <el-col :span="1" :xs="3" :sm="3" :md="3" :lg="2">
        <div class="userInfo" v-show="!isLogin">
          <router-link to="/login">登录|注册</router-link>
        </div>
      </el-col>
    </el-header>
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</template>

<script>
  import Main from '../components/Main.vue'
  export default {
    name: 'Home',
    data() {
      return {
        userName: '',
        headPic: '',
        isLogin: false,
        searchInput: '',
        activeIndex: '/components/Main',
        activeIndex2: '/components/Main',
        hasMessage: false,
        messageCount: 0,
        searchInput: ''
      };
    },
    mounted() {
      this.chekStatus();
      if (this.isLogin) {
        let timer = setInterval(() => {
          var that = this;
          that.$axios.post('share/queryMessage', {
            userId: that.$session.get('USERID')
          }).then(function(res) {
            if (res.data.code == 200) {
              if (res.data.body != 0) {
                that.hasMessage = true;
                that.messageCount = res.data.body;
              }
            }

          }).catch(function(error) {
            that.$message.error('请检查网络是否通畅');
          });

        }, 2000)
      }

    },
    methods: {
      search() {
        // this.$router.push({path:'Main',query: {content:this.searchInput}});
        Main.searchContent = this.searchInput;

      },
      clearMessage() {
        this.hasMessage = false;
      },
      signOut() {
        this.$session.clear();
        this.isLogin = false;
        this.$router.go(0);
      },
      chekStatus() {
        var that = this;
        var user = that.$session.get('USERNAME');
        if (user == '' || typeof user === 'undefined') {
          that.isLogin = false;
        } else {
          that.isLogin = true;
          that.userName = that.$session.get('USERNAME');
          that.headPic = that.$session.get('HEADPIC');
        }


      },
      computed: {

      },
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .el-header {
    float: left;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 60px;
    line-height: 60px;
    background-color: #57B8A0;
  }

  body {
    background-color: #FFF;
  }

  .el-main {
    background-color: #DCDCDC;
  }

  .menu {
    margin-top: 0;
    height: 60px;
    line-height: 60px;
  }

  #head {}

  a {
    color: white;
    text-decoration: none;
  }

  .menuItem {
    margin-left: 2%;
  }

  .block {
    margin-top: 5px;
  }

  .userInfo {
    font-weight: 600;
    font-size: 15px;
    color: #fff;
  }
</style>
